package com.example.administrator.myproject;


import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.example.administrator.myproject.Adapter.AllsortsAdapter;
import com.example.administrator.myproject.JavaBean.AllSorts;
import com.example.administrator.myproject.Utils.HttpUtil;
import com.example.administrator.myproject.Utils.ParseJsonUtil;

import org.json.JSONException;

import java.util.List;


public class AllFragment extends Fragment {

    private List<AllSorts> allSortse;
    private ListView mLv;
    private String Url;
    private AllsortsAdapter adapter;
    private boolean isBottom = false;

    public AllFragment(String Url) {
        this.Url = Url;

    }

    private String json;
    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        json = (String) msg.obj;
                        allSortse = ParseJsonUtil.ParseJsonUtil(json);
                        adapter = new AllsortsAdapter(getContext(), allSortse);
                        mLv.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

            }
        }
    };
    private SwipeRefreshLayout mRefresh;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_all, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh);
        mRefresh.setColorSchemeColors(Color.RED);

        mRefresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);

        //SwipeRefreshLayout下拉刷新
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("TAG", "===allSortses" + allSortse.size());
                allSortse.clear();
                Log.i("TAG", "===allSortsesclear" + allSortse.size());
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {

                            byte[] data = HttpUtil.loadData("http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=2&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852");
                            String json1 = new String(data);
                            allSortse = ParseJsonUtil.ParseJsonUtil(json1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                adapter.setData(allSortse);
                                mRefresh.setRefreshing(false);
                                adapter.notifyDataSetChanged();

                            }
                        }, 1000);
                    }
                }.start();


            }
        });
        mLv = (ListView) view.findViewById(R.id.lv_all);
        new Thread(new Runnable() {
            @Override
            public void run() {

                byte[] data = HttpUtil.loadData(Url);
                String json = new String(data);
                Log.i("TAG", "=====json" + json);
                Message msg = Message.obtain();
                msg.what = 1;
                msg.obj = json;
                handler.sendMessage(msg);
            }
        }).start();
        //上拉加载,用异步方式加载数据
        mLv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

                switch (i) {
                    case SCROLL_STATE_FLING:
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL:
                        break;
                    case SCROLL_STATE_IDLE:  //停止滑动

                        if (isBottom) {
                            //加载数据
                            new MyAsyncTask().execute();
                            isBottom = false;
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if ((i + i1) == i2) {

                    isBottom = true;
                }

            }
        });


    }

    //异步方式,下拉刷新数据
    class MyAsyncTask extends AsyncTask<Void, Void, List<AllSorts>> {

        private List<AllSorts> list;
        ProgressDialog dialog;

        //下拉刷新,显示dialog.dismiss();
        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(getActivity());
            dialog.setTitle("加载");
            dialog.setMessage("新数据。。。");
            dialog.show();
        }

        @Override
        protected List<AllSorts> doInBackground(Void... voids) {
            try {
                byte[] data = HttpUtil.loadData("http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=2&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852");
                String json1 = new String(data);

                list = ParseJsonUtil.ParseJsonUtil(json1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return list;//添加數據
        }

        @Override
        protected void onPostExecute(List<AllSorts> allSortses) {

            if (allSortses != null && allSortses.size() > 0) {
                allSortse.addAll(allSortses);
                adapter.notifyDataSetChanged();
//ProgressDialog延迟消失
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                }, 200);
            }
        }
    }

}
